<?php

namespace App\Http\Requests;

use Dingo\Api\Exception\ValidationHttpException;
use Dingo\Api\Http\FormRequest as BaseFormRequest;
use Illuminate\Contracts\Validation\Validator;
use Symfony\Component\HttpKernel\Exception\HttpException;

class FormRequest extends BaseFormRequest
{
    protected function failedValidation(Validator $validator)
    {
        $errors = $validator->errors()->toArray();
        $responseErrors = [];
        foreach ($errors as $property => $subErrors) {
            foreach ($subErrors as $error) {
                $error['field'] = $property;
                $responseErrors[] = $error;
            }
        }
        throw new ValidationHttpException($responseErrors);
    }
    /**
     * Handle a failed authorization attempt.
     *
     * @return void
     */
    protected function failedAuthorization()
    {
        if ($this->container['request'] instanceof Request) {
            throw new HttpException(403);
        }

        parent::failedAuthorization();
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

}
